package com.genesis.yunnanji.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.bean.MushRoomBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.DateUtils;
import com.gigamole.infinitecycleviewpager.HorizontalInfiniteCycleViewPager;
import com.lidroid.xutils.BitmapUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/4/16.
 */

public class MushRoomAdapter extends BaseQuickAdapter<MushRoomBean.ResultBean.ListBean, BaseViewHolder> {
    private Context context;
    private boolean isTop;

    public MushRoomAdapter(@Nullable List<MushRoomBean.ResultBean.ListBean> data, Context context, boolean isTop) {
        super(data);
        this.context = context;
        this.isTop = isTop;
        setMultiTypeDelegate(new MultiTypeDelegate<MushRoomBean.ResultBean.ListBean>() {
            @Override
            protected int getItemType(MushRoomBean.ResultBean.ListBean entity) {
                return entity.getListlayout();
            }
        });
        getMultiTypeDelegate()
                .registerItemType(-2, R.layout.layout_adrolllayout)
                .registerItemType(-1, R.layout.layout_adlayout)
                .registerItemType(0, R.layout.layout_newstext)
                .registerItemType(1, R.layout.layout_newsimage);


    }

    @Override
    protected void convert(BaseViewHolder helper, final MushRoomBean.ResultBean.ListBean item) {

        BitmapUtils bitmapUtils = new BitmapUtils(context);
        switch (helper.getItemViewType()) {
            case 0:
                helper.setText(R.id.tv_newstext_title, item.getTitle());
                helper.setText(R.id.tv_newstext_date, DateUtils.getTimeOfYMD(item.getAdd_time()));

                if (isTop) {
                    if (helper.getPosition() < 4) {

                        helper.getView(R.id.tv_newstext_topnews).setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_newstext_views).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.tv_newstext_topnews).setVisibility(View.GONE);
                        helper.getView(R.id.tv_newstext_views).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_newstext_views, "阅读 " + item.getView());
                    }
                } else {
                    helper.getView(R.id.tv_newstext_topnews).setVisibility(View.GONE);
                    helper.getView(R.id.tv_newstext_views).setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_newstext_views, "阅读 " + item.getView());
                }

                if (TextUtils.isEmpty(item.getCover_img())) {
                    helper.getView(R.id.iv_newstext_icon).setVisibility(View.GONE);
                } else {
                    bitmapUtils.display(helper.getView(R.id.iv_newstext_icon), GenesisApiConfig.PIC_HOST + item.getCover_img());
                }

                break;
            case 1:
                helper.setText(R.id.tv_newsimage_title, item.getTitle());
                bitmapUtils.display(helper.getView(R.id.iv_newsimage_icon1), GenesisApiConfig.PIC_HOST + item.getCover_img());
                bitmapUtils.display(helper.getView(R.id.iv_newsimage_icon2), GenesisApiConfig.PIC_HOST +item.getCover_img2());
                bitmapUtils.display(helper.getView(R.id.iv_newsimage_icon3), GenesisApiConfig.PIC_HOST +item.getCover_img3());
                helper.setText(R.id.tv_newsimage_date, DateUtils.getTimeOfYMD(item.getAdd_time()));

                if (isTop) {
                    if (helper.getPosition() < 4) {

                        helper.getView(R.id.tv_newsimage_topnews).setVisibility(View.VISIBLE);
                        helper.getView(R.id.tv_newsimage_views).setVisibility(View.GONE);
                    } else {
                        helper.getView(R.id.tv_newsimage_topnews).setVisibility(View.GONE);
                        helper.getView(R.id.tv_newsimage_views).setVisibility(View.VISIBLE);
                        helper.setText(R.id.tv_newsimage_views, "阅读 " + item.getView());
                    }
                } else {
                    helper.getView(R.id.tv_newsimage_topnews).setVisibility(View.GONE);
                    helper.getView(R.id.tv_newsimage_views).setVisibility(View.VISIBLE);
                    helper.setText(R.id.tv_newsimage_views, "阅读 " + item.getView());
                }

                break;
            case -1:
                bitmapUtils.display(helper.getView(R.id.iv_adlayout), GenesisApiConfig.PIC_HOST + item.getCover_img());
                break;
            case -2:
                HorizontalInfiniteCycleViewPager horizontalInfiniteCycleViewPager = helper.getView(R.id.hor_pager);
                HorizontalPagerAdapter2 coverFlowAdapter = new HorizontalPagerAdapter2(context, item.getAdvert_list());
                horizontalInfiniteCycleViewPager.setAdapter(coverFlowAdapter);
                break;

        }


    }
}
